package selenium;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class baiduFenCi {
//乐视采集系列
	public static void main(String[] args) {
		final WebDriver driver = new ChromeDriver();
		String textString = "开车不抽烟，抽烟撞美女，值不值";
		try {
			//driver.manage().window().maximize();
			String sougou = "http://www.sogou.com/web?query="+textString+"&page=";
			String baidu = "https://www.baidu.com/s?wd="+textString+"&pn=";
			Map<String,Integer> map = new HashMap<String,Integer>();
			for (int j = 1; j < 3; j++) {
				String[] arg = new String[2];
				arg[0] = sougou +j;//搜狗分析
				arg[1] = baidu+10*j;//百度分析
				for (int ii = 0; ii < 1; ii++) {
					if (arg[ii] != null) {
						driver.get(arg[ii]);// 百度和搜狗分析
						List<WebElement> a1 = driver.findElements(By.tagName("em"));
						for (WebElement webElement : a1) {
							String text = webElement.getText().replace(",", "").replace("《", "").replace("》",
									"");
							if (map.get(text) == null) {
								map.put(text, 1);
							} else {
								map.put(text, map.get(text) + 1);
							}
						}
					}
				}
			}
			
			 System.out.println("标题："+textString);
			 System.out.println("分析结果:");
			 
			// 升序比较器
			Comparator<Map.Entry<String, Integer>> valueComparator = new Comparator<Map.Entry<String, Integer>>() {
				@Override
				public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
					// TODO Auto-generated method stub
					return o2.getValue() - o1.getValue();
				}
			};
			 // map转换成list进行排序
			 List<Map.Entry<String, Integer>> list = new ArrayList<Map.Entry<String,Integer>>(map.entrySet());
			 // 排序
			 Collections.sort(list,valueComparator);
			 // 默认情况下，TreeMap对key进行升序排序
			 System.out.println("------------分析频次按照降序排列--------------------");
			 StringBuilder myStringBuilder = new StringBuilder("");
			 int i = 1;
			 for (Map.Entry<String, Integer> entry : list) {
			 System.out.println(entry.getKey() + ":" + entry.getValue());
				if (i < 8) {
					myStringBuilder.append(entry.getKey());
					myStringBuilder.append("/");
					i++;
				}
			}
			 System.out.println("领域分类切词："+myStringBuilder.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		driver.quit();
	}

	// 方法1：

	public void method1() {
		FileWriter fw = null;
		try {
			// 如果文件存在，则追加内容；如果文件不存在，则创建文件
			File f = new File("E:\\dd.txt");
			fw = new FileWriter(f, true);
		} catch (IOException e) {
			e.printStackTrace();
		}
		PrintWriter pw = new PrintWriter(fw);
		pw.println("追加内容");
		pw.flush();
		try {
			fw.flush();
			pw.close();
			fw.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	// 方法2：

	public static void method2(String file, String conent) {
		BufferedWriter out = null;
		try {
			out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, true)));
			out.write(conent + "\r\n");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				out.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	// 方法3：

	public static void method3(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// 文件长度，字节数
			long fileLength = randomFile.length();
			// 将写文件指针移到文件尾。
			randomFile.seek(fileLength);
			randomFile.writeBytes(content + "\r\n");
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
